package ecoiot.com.testproject.listview;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ecoiot.com.testproject.R;

/**
 * Created by developer on 18-1-19.
 */

public class TestRecyclerActivity extends AppCompatActivity {
    private final int TYPE_CHECK_BOX = 123;
    private final int TYPE_RADIO_BUTTON = 456;


    String[] keys = new String[] {
            "白日依山尽",
            "黄河入海流",
            "欲穷千里目",
            "更上一层楼",
            "离离原上草",
            "一岁一枯荣",
            "野火烧不尽",
            "春风吹又生",
            "远芳侵古道",
            "晴翠接荒城",
    };

    Map<String, List<String>> dataList;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_rv);

        initList();

//        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setLayoutManager(new GridLayoutManager(this, 1));
//        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.HORIZONTAL));
        recyclerView.setAdapter(new MyAdapter());

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.menu_clear) {
            if (recyclerView != null) {
                recyclerView.setAdapter(new MyAdapter());
            }
        }

        return super.onOptionsItemSelected(item);
    }

    private void initList() {
        dataList = new HashMap<>();

        List<String> titleList1 = new ArrayList<>();
        List<String> titleList2 = new ArrayList<>();
        List<String> titleList3 = new ArrayList<>();
        List<String> titleList4 = new ArrayList<>();
        List<String> titleList5 = new ArrayList<>();
        List<String> titleList6 = new ArrayList<>();
        List<String> titleList7 = new ArrayList<>();
        List<String> titleList8 = new ArrayList<>();
        List<String> titleList9 = new ArrayList<>();
        List<String> titleList10 = new ArrayList<>();

        for (int i = 0; i < 4; i ++) {
            titleList1.add("第" + i + "个标签");
            titleList6.add("第" + i + "个标签");
        }

        for (int i = 0; i < 5; i ++) {
            titleList2.add("第" + i + "个标签");
            titleList10.add("第" + i + "个标签");
        }

        for (int i = 0; i < 3; i ++) {
            titleList3.add("第" + i + "个标签");
            titleList8.add("第" + i + "个标签");
            titleList5.add("第" + i + "个标签");

        }

        for (int i = 0; i < 2; i ++) {
            titleList4.add("第" + i + "个标签");
            titleList7.add("第" + i + "个标签");
            titleList9.add("第" + i + "个标签");
        }

        dataList.put(keys[0], titleList1);
        dataList.put(keys[1], titleList2);
        dataList.put(keys[2], titleList3);
        dataList.put(keys[3], titleList4);
        dataList.put(keys[4], titleList5);
        dataList.put(keys[5], titleList6);
        dataList.put(keys[6], titleList7);
        dataList.put(keys[7], titleList8);
        dataList.put(keys[8], titleList9);
        dataList.put(keys[9], titleList10);

    }

    public int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }


    private class MyAdapter extends RecyclerView.Adapter {

        @Override
        public int getItemViewType(int position) {
            if (position == 0 || position == 3 || position == 4) {
                return TYPE_RADIO_BUTTON;
            }else {
                return TYPE_CHECK_BOX;
            }
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == TYPE_CHECK_BOX) {
                return new CheckBoxHolder(View.inflate(TestRecyclerActivity.this, R.layout.item_recycler_cb, null));
            }else if (viewType == TYPE_RADIO_BUTTON) {
                return new RadioButtonHolder(View.inflate(TestRecyclerActivity.this, R.layout.item_recycler_rb, null));
            }else {
                return null;
            }


        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            String key = keys[position];
            final List<String> items = dataList.get(key);

            if (holder instanceof RadioButtonHolder) {
                RadioButtonHolder rbHolder = (RadioButtonHolder) holder;
                rbHolder.tvTitle.setText(key);

                for (String itemTitles : items) {
                    RadioButton button = new RadioButton(TestRecyclerActivity.this);
                    button.setText(itemTitles);
                    button.setPadding(5, 5, 5, 5);
                    button.setBackgroundResource(R.drawable.selector_btn);
                    button.setButtonDrawable(null);

                    RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    layoutParams.setMargins(dp2px(10), 0, dp2px(10), 0);
                    rbHolder.rgGroup.addView(button, layoutParams);
                }

            }else if (holder instanceof CheckBoxHolder) {
                CheckBoxHolder cbHolder = (CheckBoxHolder) holder;
                cbHolder.tvTitle.setText(key);

                cbHolder.gvItem.setAdapter(new BaseAdapter() {
                    @Override
                    public int getCount() {
                        return items.size();
                    }

                    @Override
                    public Object getItem(int position) {
                        return null;
                    }

                    @Override
                    public long getItemId(int position) {
                        return 0;
                    }

                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        MyGridHolder gridHolder;

                        if (convertView == null) {
                            convertView = View.inflate(TestRecyclerActivity.this, R.layout.item_grid_list, null);
                            gridHolder = new MyGridHolder();
                            gridHolder.checkBox = convertView.findViewById(R.id.item_grid_cb);
                            convertView.setTag(gridHolder);
                        }else {
                            gridHolder = (MyGridHolder) convertView.getTag();
                        }

                        gridHolder.checkBox.setText(items.get(position));

                        return convertView;
                    }
                });

            }
        }

        @Override
        public int getItemCount() {
            return dataList.size();
        }

        class CheckBoxHolder extends RecyclerView.ViewHolder {
            TextView tvTitle;
            MyGridView gvItem;

            CheckBoxHolder(View itemView) {
                super(itemView);
                tvTitle = itemView.findViewById(R.id.item_recycler_tv1);
                gvItem = itemView.findViewById(R.id.item_recycler_gv);
            }
        }

        class RadioButtonHolder extends RecyclerView.ViewHolder {
            TextView tvTitle;
            RadioGroup rgGroup;

            RadioButtonHolder(View itemView) {
                super(itemView);
                tvTitle = itemView.findViewById(R.id.item_recycler_tv2);
                rgGroup = itemView.findViewById(R.id.item_recycler_rg);
            }
        }

        class MyGridHolder {
            CheckBox checkBox;
        }

    }
}
